/**
 * Filename: GAGLAlertWindow.js
 * Author: Sunilla Sarfraz
 * Modified: Zaki Ullah
 * Date Created: --
 * Date Updated: --
 */
// Initializing AlertWindow 
// there are 5 parameters passed to constructor below are the details of parameters
// 1.initWithTitle				: it is first parameter. it will take title string and set AlertWindow title
// 2.message       				: it will take message string and set Message in AlertWindow
// 3.delegate 	   				: setting delegate.
// 4.cancelButtonTitle			: it will take cancel button title string which should be localized string
// 5.otherButtonTitles			: it will take ok button title string which should be localized string

// setter functions
// there are 9 setter function through which you can set button text, title text and message text colors.
// it is also allow you to set background and button normal and highlighted images path
// you can also set type of your alertWindow and type detail is given below

// there are two type of alert view
// 1."ok" which means AlertWindow with ok button only
// 2."okwithcancel" means AlertWindow with ok and cancel button both 
// by default it takes type "ok"


//Default type for alert window
var kALERT_WINDOW_TYPE = "ok"

//AlertWindow button image default paths
var kALERT_WINDOW_BACKGROUND  = "./Content/images/gamelayer/controllayer/main-menu-background.png"

// Ok buttons
var kALERT_WINDOW_OK_NORMAL ="./Content/images/gamelayer/ok-normal.png"
var kALERT_WINDOW_OK_SELECTED ="./Content/images/gamelayer/ok-selected.png"

// Cancel buttons
var kALERT_WINDOW_CANCEL_NORMAL ="./Content/images/gamelayer/cancel-normal.png"
var kALERT_WINDOW_CANCEL_SELECTED ="./Content/images/gamelayer/cancel-selected.png"


exports.GAGLAlertWindow = GL2.Node.subclass({
	
	classname: 'GAGLAlertWindow',
	initialize: function(initWithTitle,message,delegate,cancelButtonTitle,otherButtonTitles)
	{
	
		this.w							= null;
		this.h							= null;
		this._tagValue					= null;
		
		this._title 					= null;
		this._message 					= null;
		this._delegate 					= null;
		this.cancelButtonTitle 			= null;
		this._otherButtonTitles			= null;
		
		this._okButton					= null;
		this._cancelButton 				= null;
		this._messageLabel 	    		= null;
		this._titleLabel	    		= null;
		this._backgroundImage 			= null;
		this._type						= null;
		
		this._backgroundImagePath		= null;
		this._okImageNormalPath 		= null;
		this._okImageSelectedPath 		= null;
		this._cancelImageNormalPath		= null;
		this._cancelImageSelectedPath	= null;
    	
    	this._buttonsColor				= null;
    	this._titleColor				= null;
    	this._messageColor				= null;
		
		this._background 				= null;
		this.myMessageListener 			= null;
		
		// initializing data members with parameters 
		if (initWithTitle == null) {
			initWithTitle = "Message";
			}
		
		if (initWithTitle.length > 11) {
			this._title = initWithTitle.substring(0, 10);
			} 
		else 
		{
			this._title = initWithTitle;
		}
		
		this._message = message;
		this._delegate = delegate;
		this.cancelButtonTitle = cancelButtonTitle;
		this._otherButtonTitles= otherButtonTitles;	
		
		// setting images default path
		this._backgroundImagePath		=kALERT_WINDOW_BACKGROUND;
		this._okImageNormalPath			=kALERT_WINDOW_OK_NORMAL;
		this._okImageSelectedPath		=kALERT_WINDOW_OK_SELECTED;
		this._cancelImageNormalPath	 	=kALERT_WINDOW_CANCEL_NORMAL;
		this._cancelImageSelectedPath	=kALERT_WINDOW_CANCEL_SELECTED;
		
		// setting Default type
		this._type=kALERT_WINDOW_TYPE;
		
		// setting default colors
		this._buttonsColorRed=1;
		this._buttonsColorGreen=1;
		this._buttonsColorBlue=1;
		
		this._titleColorRed=0;
		this._titleColorGreen=0;
		this._titleColorBlue=0;
		
		this._messageColorRed=0; 
		this._messageColorGreen=0;
		this._messageColorBlue=0
		// setting frame of this view
		this.w = Core.Capabilities.getScreenWidth();
		this.h = Core.Capabilities.getScreenHeight();
	
		this.makeTouchSwallower();// fullscreen touch target to swallow the touches beyond the attribute window 

    	
		return this;
	},
	loadGl: function()
	{
		this._background = new GL2.Sprite();		
		this._background.setImage(this._backgroundImagePath,[this.adjustWidthWithDevice(320),this.adjustHeightWithDevice(229)],[0,.5]);
		this._background.setPosition(this.getAdjustedX(0),this.getAdjustedY(235));
        this.addChild(this._background);
       	
		this._okButton = new GLButton(this._okImageNormalPath,this._okImageSelectedPath, this.adjustWidthWithDevice(73),this.adjustHeightWithDevice(37),this.clickedButtonOk.bind(this),this,100);
		this._okButton.setPosition(this.getAdjustedX(160),this.getAdjustedY(290));
		
		this._okButtonText = new GL2.Text();
		this._okButtonText.setText(this._otherButtonTitles);
		this._okButtonText.setAnchor(0,0);
		this._okButtonText.setFontSize(this.getAdjustedFontSize(80));
		this._okButtonText.setSize([this.adjustWidthWithDevice(71),this.adjustHeightWithDevice(55)]);
		this._okButtonText.setPosition(this.getAdjustedX(213),this.getAdjustedY(181));
		this._okButtonText.setColor(this._buttonsColorRed,this._buttonsColorGreen,this._buttonsColorBlue);
		//this._okButtonText.setColor(1,1,1);
		this._okButtonText.setDepth(100);
		this.addChild(this._okButtonText);		

		if(this._type == "okwithcancel")
		{
			this._okButton.setPosition(this.getAdjustedX(120),this.getAdjustedY(290));
			this._okButtonText.setPosition(this.getAdjustedX(173),this.getAdjustedY(173));
			
			this._cancelButton = new GLButton(this._cancelImageNormalPath,this._cancelImageSelectedPath, this.adjustWidthWithDevice(73),this.adjustHeightWithDevice(37),this.clickedButtonCancel.bind(this),this,100);
			this._cancelButton.setPosition(this.getAdjustedX(205),this.getAdjustedY(290));
			
			this._cancelButtonText = new GL2.Text();
			this._cancelButtonText.setText(this.cancelButtonTitle);
			this._cancelButtonText.setAnchor(0,0);
			this._cancelButtonText.setFontSize(this.getAdjustedFontSize(80));
			this._cancelButtonText.setSize([this.adjustWidthWithDevice(71),this.adjustHeightWithDevice(55)]);
			this._cancelButtonText.setPosition(this.getAdjustedX(253),this.getAdjustedY(173));
			this._cancelButtonText.setColor(this._buttonsColorRed,this._buttonsColorGreen,this._buttonsColorBlue);
			//this._cancelButtonText.setColor(1,1,1);
			this._cancelButtonText.setDepth(100);
			this.addChild(this._cancelButtonText);
		}
		
		this._messageLabel = new GL2.Text();
    	this._messageLabel.setText(this._message);
    	this._messageLabel.setSize(this.adjustWidthWithDevice(260),this.adjustHeightWithDevice(220));
    	this._messageLabel.setAnchor(.5,.5);
    	this._messageLabel.setFontSize(this.getAdjustedFontSize(100));
    	this._messageLabel.setColor(this._messageColorRed,this._messageColorGreen,this._messageColorBlue);
    	//this._message.setColor(1,1,1);
    	this._messageLabel.setPosition(this.getAdjustedX(160),this.getAdjustedY(195));
    	this.addChild(this._messageLabel);
    	
    	
    	this._titleLabel = new GL2.Text();
    	this._titleLabel.setText(this._title);
    	this._titleLabel.setAnchor(.5,.5);
    	this._titleLabel.setFontSize(this.getAdjustedFontSize(120));
    	this._titleLabel.setColor(this._titleColorRed,this._titleColorGreen,this._titleColorBlue);
    	//this._title.setColor(1,1,1);
    	this._titleLabel.setPosition(this.getAdjustedX(160),this.getAdjustedY(145));
    	this.addChild(this._titleLabel);
    	
		
	},
	show: function()
	{
		this.loadGl();
		GL2.Root.addChild(this);
	},
	
	
	makeTouchSwallower: function()
    {
    	var myMessageListener = 0;
    	this._dissolveTouchesTarget = new GL2.TouchTarget();
    	this._dissolveTouchesTarget.setAnchor(0,0);
		this._dissolveTouchesTarget.setSize(this.getAdjustedX(this.w),this.getAdjustedY(this.h));
        this._dissolveTouchesTarget.setPosition(this.getAdjustedX(0),this.getAdjustedY(0));
        myMessageListener = new Core.MessageListener();
		this._dissolveTouchesTarget.getTouchEmitter().addListener(myMessageListener, this.dissolveTouches.bind(this),kTouchSwallower); //@sunilla make this 10 go to Constants.
		this.addChild(this._dissolveTouchesTarget);
    },
    
    
    
    dissolveTouches: function()
    {
    	return true;
    },
	
	
	// on click call backs
	clickedButtonOk: function()
	{
		this.clickedButtonAtIndex(0);
	 	return true;
	},
	
	clickedButtonCancel: function()
	{
		this.clickedButtonAtIndex(1);
	 	return true;	
	},
	
	
	clickedButtonAtIndex: function(buttonIndex)
	{
		
		if( this._delegate)		
		{
			this._delegate.clickedButtonAtIndex(buttonIndex,this);
		}
		
		this.destroy();	
		
	},
	

	////////////////////////////////////////////////////////////
	
	
	//getter
	getTagValue: function()
	{
		return this._tagValue;
	},
	
	
	//setters
	
	setTagValue: function(value)
	{
		this._tagValue = value;
	},
	
	setType : function(type)
	{
		this._type=type;
	},
	setButtonColor : function(red,green,blue)
	{
		this._buttonsColorRed	=red;
		this._buttonsColorGreen	=green;
		this._buttonsColorBlue	=blue;
		
	},
	
	setTitleColor : function(red,green,blue)
	{
		this._titleColorRed		=red;
		this._titleColorGreen	=green;
		this._titleColorBlue	=blue;
	},
	
	setMessageColor : function(red,green,blue)
	{
		this._messageColorRed	=red;
		this._messageColorGreen	=green;
		this._messageColorBlue	=blue;
	},

	setBackgroundImagePath: function(path)
	{
		this._backgroundImagePath =path;
	},
	
	setOkImageNormalPath: function(path)
	{
		this._okImageNormalPath =path;
	},
	
	setOkImageSelectedPath: function(path)
	{
		this._okImageSelectedPath =path;
	},
	
	setCancelImageNormalPath: function(path)
	{
		this._cancelImageNormalPath =path;
	},
	
	setCancelImageSelectedPath: function(path)
	{
		this._cancelImageSelectedPath =path;
	},
		
	////////////////////////////////////////////////////////////
	
getAdjustedX: function(x) {
	
	var width = 0 , newWidth = 0 ;
	width = Core.Capabilities.getScreenWidth();
	newWidth = (width / 320) * x;
	
	return newWidth;
	
},

getAdjustedY: function(y) 
{
	var height = 0 , newHeight = 0;
	height = Core.Capabilities.getScreenHeight();
	newHeight = (height / 480) * y;
	
	return newHeight;
},

adjustWidthWithDevice: function(aWidth)
{
	var w = 0, calWidth = 0;
	
	w = Core.Capabilities.getScreenWidth();
	
	calWidth = aWidth * (w/320);
		//////NgLogE('Bandri Width:'+aWidth +'-->'+calWidth +'device W:'+w);
	return calWidth;			
},
adjustHeightWithDevice: function(aHeight)
{
	var h = 0 , calHeight = 0;
	
	h = Core.Capabilities.getScreenHeight();
	
	calHeight = aHeight * (h/480);
		//////NgLogE('Bandri Width:'+aHeight +'-->'+calHeight +'device H:'+h);
	return calHeight;		
	
},

getAdjustedFontSize: function(fieldHeight) {
	return this.getAdjustedX(fieldHeight / 2) / aspectRatioFactor.d;
},
	////////////////////////////////////////////////////////////

destroy: function()
	{
		GL2.Root.removeChild(this);
		
		this._dissolveTouchesTarget.destroy();
		
		
		if(this._background != null)
		{
			this.removeChild(this._background);
			this._background.destroy();
			this._background=null;
		}
		
		if(this._messageLabel != null)
		{
			this.removeChild(this._messageLabel);
			this._messageLabel.destroy();
			this._messageLabel=null;
			
		}
		if(this._titleLabel != null)
		{
			this.removeChild(this._titleLabel);
			this._titleLabel.destroy();
			this._titleLabel=null;
		}
		if(this._okButton != null)
		{
			this._okButton.destroy();
			this._okButton=null;
		}
		if(this._okButtonText != null)
		{
			this.removeChild(this._okButtonText);
			this._okButtonText.destroy();
			this._okButtonText=null;
			
		}
		if(this._cancelButton != null)
		{
			this._cancelButton.destroy();
			this._cancelButton=null;
			
		}
		if(this._cancelButtonText != null)
		{
			this.removeChild(this._cancelButtonText);
			this._cancelButtonText.destroy();
			this._cancelButtonText=null;
			
		}
	}
});